package com.blockchain.securewallet.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.dialog.IDialogListener;
import com.blockchain.securewallet.dialog.IDialogTipsListener;
import com.blockchain.securewallet.dialog.UniversalDialog;
import com.blockchain.securewallet.tools.WalletTools;
import com.blockchain.securewallet.utils.Logger;
import com.blockchain.securewallet.utils.StringUtil;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeeSettingActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_btc_count)
    TextView tvBtcCount;

    @BindView(R.id.tv_eth_count)
    TextView tvEthCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fee_string);
        ButterKnife.bind(this);
        tvTitle.setText("手续费");
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        long btcFee = WalletApp.getInstance().getUserBitCoinFee();
        BigDecimal btcBig = BigDecimal.valueOf(btcFee).divide(BigDecimal.valueOf(100_000_000));
        //BigDecimal btcBig = BigDecimal.valueOf(btcFee).multiply(BigDecimal.valueOf(1000)).divide(BigDecimal.valueOf(100_000_000));
        //toPlainString平滑度过，使用toString会表示成科学计数法
        tvBtcCount.setText(btcBig.toPlainString() + "");

        long gasPrice = WalletApp.getInstance().getUserGasPrice();
//        long gasLimit = WalletApp.getInstance().getGasLimit();

        //现在的单位是Gwei了
        BigDecimal ethBig = BigDecimal.valueOf(gasPrice);
//        BigDecimal ethBig = BigDecimal.valueOf(gasPrice).multiply(BigDecimal.valueOf(gasLimit)).divide(BigDecimal.valueOf(1000_000_000));
        tvEthCount.setText(ethBig.toPlainString() + "");
    }

    @OnClick(R.id.cv_btc_fee)
    void updateBtcFee() {

        UniversalDialog dialog = UniversalDialog.showBtcFeeDialog(getSupportFragmentManager(), new IDialogTipsListener() {
            @Override
            public void onItemClick(View view, String... texts) {
                if (texts.length != 1) {
                    return;
                }

              /*  String userBTCFee = WalletTools.getUserBTCFee();
                EditText et_fee = view.findViewById(R.id.edit_fee);
                //自定义手续费
                et_fee.setText(userBTCFee);*/

                try {
                    //注意这里的没有除以1000了
                    BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(texts[0])).multiply(BigDecimal.valueOf(100_000_000));
                    //BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(texts[0])).multiply(BigDecimal.valueOf(100_000_000)).divide(BigDecimal.valueOf(1000));
                    WalletApp.getInstance().saveUserBitCoinFee(bigDecimal.longValue());

                } catch (Exception e) {
                    Logger.ds("err: " + e);
                }

                long btcFee = WalletApp.getInstance().getUserBitCoinFee();
                BigDecimal btcBig = BigDecimal.valueOf(btcFee).divide(BigDecimal.valueOf(100_000_000));
                //BigDecimal btcBig = BigDecimal.valueOf(btcFee).multiply(BigDecimal.valueOf(1000)).divide(BigDecimal.valueOf(100_000_000));
                tvBtcCount.setText(btcBig.toPlainString() + "");
            }
        }, WalletTools.getUserBTCFee());
    }

    @OnClick(R.id.cv_eth_fee)
    void updateEthFee() {

        UniversalDialog dialog = UniversalDialog.showEthFeeDialog(getSupportFragmentManager(), new IDialogTipsListener() {
            @Override
            public void onItemClick(View view, String... texts) {
                if (texts.length != 3) {
                    return;
                }

               /* String userETHFee = WalletTools.getUserETHFee();
                EditText et_fee = view.findViewById(R.id.edit_fee);
                //自定义手续费
                et_fee.setText(userETHFee);*/

                if (!TextUtils.isEmpty(texts[2])) {
                    try {
                        long nonce = BigDecimal.valueOf(Double.valueOf(texts[2])).longValue();
                        WalletApp.getInstance().saveUserNonce(nonce);
                    } catch (Exception e) {
                        Log.i("ywLog", "nonce illegal", e);
                    }
                }
                
                if (!TextUtils.isEmpty(texts[0])) {
                    try {
                        long price = BigDecimal.valueOf(Double.valueOf(texts[0])).longValue();
                        WalletApp.getInstance().saveUserGasPrice(price);
                    } catch (Exception e) {
                        Log.i("ywLog", "gas illegal", e);
                    }
                }

                if (!TextUtils.isEmpty(texts[1])) {
                    try {
                        long limit = BigDecimal.valueOf(Double.valueOf(texts[1])).longValue();
                        WalletApp.getInstance().saveUserGasLimit(limit);
                    } catch (Exception e) {
                        Log.i("ywLog", "gasLimit illegal", e);
                    }
                }
                
                long gasPrice = WalletApp.getInstance().getUserGasPrice();
                long gasLimit = WalletApp.getInstance().getUserGasLimit();
                //注意这里与gasLimit的关系，暂时注释掉
                //BigDecimal ethBig = BigDecimal.valueOf(gasPrice).multiply(BigDecimal.valueOf(gasLimit)).divide(BigDecimal.valueOf(1000_000_000));
                
                //直接显示Gas Price
                BigDecimal ethBig = BigDecimal.valueOf(gasPrice);
                tvEthCount.setText(ethBig.toPlainString() + "");
            }
        }, WalletTools.getUserETHFee(), WalletTools.getETHGasLimit(), WalletTools.getETHNonce());
    }

}
